/*
 * @ : Copyright (c) 2021 Phytium Information Technology, Inc. 
 *  
 * SPDX-License-Identifier: Apache-2.0.
 * 
 * @Date: 2021-07-09 08:08:39
 * @LastEditTime: 2021-07-09 13:55:27
 * @Description:  This files is for 
 * 
 * @Modify History: 
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 */
#include <stdio.h>
#include <fsleep.h>
#include <generic_timer.h>
#include "interrupt.h"
#include "gicv3.h"
#include "parameters.h"
#include "fwdt.h"
#include "fwdt_hw.h"

FWdtCtrl wdt_ctrl;

/* wdt num: 0 or 1 */
u8 wdt_id = 0;

/**
 * @name: WdtInterrupt
 * @msg:  This function handle wdt timeout interrupt, use it to refresh wdt.
 * @return {void} 
 * @param  {s32} vector, the interrupt number
 * @param  {void} *param, pointer to a WdtCtrl structure that contains
  *                the configuration information for the specified wdt module.
 */
void FWdtInterrupt(s32 vector, void *param)
{
    FWdtRefresh((FWdtCtrl *)param);
    printf("wdt refresh.\n");
}

/**
 * @name: WdtRefreshTest
 * @msg:  Set wdt interrupt to refresh wdt, set timeout value, start wdt.
 * @return {void} 
 * @param {WdtCtrl} *pctrl, pointer to a WdtCtrl structure that contains
  *                the configuration information for the specified wdt module.
 */

void FWdtRefreshTest(FWdtCtrl *pctrl)
{
    FWdtConfig *pconfig = &pctrl->config;

    /* interrupt init */
    InterruptSetPriority(pconfig->irq_num, pconfig->irq_prority);
    InterruptInstall(pconfig->irq_num, FWdtInterrupt, (void*)pctrl, pconfig->instance_name);
    InterruptUmask(pconfig->irq_num);

    FWdtSetTimeout(pctrl, 6);

    FWdtStart(pctrl);
   
}

/**
 * @name: WdtNoRefreshTest
 * @msg:  Set wdt timeout value, start wdt, no refresh.
 * @return {void} 
 * @param {WdtCtrl} *pctrl,  pointer to a WdtCtrl structure that contains
  *                the configuration information for the specified wdt module.
 */
void FWdtNoRefreshTest(FWdtCtrl *pctrl)
{
    FWdtConfig *pconfig = &pctrl->config;

    FWdtSetTimeout(pctrl, 6);

    FWdtStart(pctrl);
}

int main()
{
    u32 count = 0;
    printf("ft wdt test.\n");

    
    FWdtCtrl *pctrl = &wdt_ctrl;
   
    pctrl->config = *FWdtLookupConfig(wdt_id);

    FWdtRefreshTest(pctrl);
    
    while(1){

        printf("wcvh=%#x, wcvl=%#x, tick=%#x, wor=%#x\n", FWdtReadWCVH(pctrl),\
        FWdtReadWCVL(pctrl), GenericTimerRead(), FWdtReadWOR(pctrl));

        printf("timeleft=%d, wcs=%#x, count=%d\n", FWdtGetTimeleft(pctrl), FWdtReadWCS(pctrl), count);
        
        fsleep_millisec(1000);
        count++;
    };
    return 0;
}
